package com.clz.hyperit.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;




import com.clz.hyperit.entity.Position;
import com.clz.hyperit.entity.Tag;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.exception.ApplicationRunTimeException;
import com.clz.hyperit.service.PositionService;
import com.clz.hyperit.service.UserDetailService;
import com.clz.hyperit.util.FileUploadUtil;
import com.clz.hyperit.util.LogarithmUtil;
import com.clz.hyperit.util.ResultMessage;
import com.clz.hyperit.vo.PageInfo;
import com.clz.hyperit.vo.UserDetailVO;


@RestController
@RequestMapping(value = "/userdetail")
public class UserDetailController {
	@Autowired
	private UserDetailService userDetailService;
	@Autowired
	private PositionService positionService;
	private ResultMessage result = null;
	
	private final Logger logger = LoggerFactory.getLogger(UserDetailController.class);
	/**
	 * 获取用户信息
	 * @param userDetail_id
	 * @return
	 */
	@RequestMapping(value = "/getUserInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getUserInfo(Integer userDetail_id){
		result = new ResultMessage();
		int level;
		UserDetailVO ud_vo = null;
		result.setServiceResult(true);
		logger.debug("获取用户信息");
		UserDetail ud = userDetailService.getUserInfo(userDetail_id);
		if(ud.getExp()<=0){
			level = 0;
		}
		else{
			level = (int)LogarithmUtil.log(ud.getExp(),2);
		}
		Position position = positionService.findById(ud.getPosition_id());
		if(position!=null){
			 ud_vo = new UserDetailVO(ud,level,position.getName());
		}
		else{
			ud_vo = new UserDetailVO(ud,level,"无职位");
		}
		result.getResultParm().put("userDetail", ud_vo);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 上传头像
	 * @param userDetail_id
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/uploadHeadImg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> uploadHeadImg(Integer userDetail_id,MultipartFile file,HttpServletRequest request){
		UserDetail ud = new UserDetail();
		if ( userDetail_id== 0||userDetail_id==null) {
			throw new ApplicationRunTimeException("用户详情id不能为空");
		}
		result = new ResultMessage();
		if (file!=null) {
			logger.info("file不为空，开始处理上传头像");
			String headPath = FileUploadUtil.NewFileUpload(request, file,"headpath");
			logger.info("上传头像访问地址："+ headPath);
		
			ud.setId(userDetail_id);
			ud.setHead(headPath);
		} 
		userDetailService.updateUserDetail(ud);
		result.setServiceResult(true);
		result.setResultInfo("上传头像成功");
		result.getResultParm().put("headpath", userDetailService.getUserInfo(ud.getId()).getHead());
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 更新用户信息
	 * @param userdetail
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateUserDetail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> updateUserDetail(UserDetail userdetail ,MultipartFile file,HttpServletRequest request){
		UserDetail ud = userdetail;
		if ( userdetail.getId()== 0||userdetail.getId()==null) {
			throw new ApplicationRunTimeException("用户详情id不能为空");
		}
		result = new ResultMessage();
		if (file!=null) {
			logger.info("file不为空，开始处理上传头像");
			String headPath = FileUploadUtil.NewFileUpload(request, file,"headpath");
			logger.info("上传头像访问地址："+ headPath);
			ud.setHead(headPath);
		} 
		
		userDetailService.updateUserDetail(ud);
		result.setServiceResult(true);
		result.setResultInfo("更新用户详情成功");
		result.getResultParm().put("userdetail", userDetailService.getUserInfo(ud.getId()));
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 签到
	 * @param userdetail_id
	 * @return
	 */
	@RequestMapping(value = "/sign", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> sign(Integer userdetail_id){
		result = new ResultMessage();
		Boolean flag = userDetailService.isSign(userdetail_id);
		if(flag){
			result.setResultInfo("签到成功");
			UserDetail ud =userDetailService.getUserInfo(userdetail_id);
			int level = (int)LogarithmUtil.log(ud.getExp(),2);
			UserDetailVO ud_vo = new UserDetailVO(ud,level,null);
			result.getResultParm().put("userdetail",ud_vo);
		}
		else{
			result.setResultInfo("今天已签到");
		}
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 获得所有用户-时间排序
	 * @return
	 */
	@RequestMapping(value = "/getAllUserInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getAllUserInfo(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<UserDetailVO> ud_list = userDetailService.getAllUserInfo(pageInfo);
		result.getResultParm().put("userDetail_list", ud_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 封停帐号
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteUser", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deleteUser(Integer id){
		result = new ResultMessage();
		result.setServiceResult(true);
		userDetailService.deleteUser(id);
		result.setResultInfo("封停用户成功");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 解封帐号
	 */
	@RequestMapping(value = "/unfreeze", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> unfreeze(Integer id){
		result = new ResultMessage();
		result.setServiceResult(true);
		userDetailService.updateUnfreeze(id);
		result.setResultInfo("解冻用户成功");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 排序->按照文章数
	 * @return
	 */
	@RequestMapping(value = "/findUdByArticle", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findUdByArticle(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<UserDetailVO> ud_list = userDetailService.findUdByArticle(pageInfo);
		result.getResultParm().put("userDetail_list", ud_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 排序->按照粉丝数
	 * @return
	 */
	@RequestMapping(value = "/findUdByFans", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findUdByFans(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<UserDetailVO> ud_list = userDetailService.findUdByFans(pageInfo);
		result.getResultParm().put("userDetail_list", ud_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 排序->按照经验
	 * @return
	 */
	@RequestMapping(value = "/findUdByEXP", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findUdByEXP(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<UserDetailVO> ud_list = userDetailService.findUdByEXP(pageInfo);
		result.getResultParm().put("userDetail_list", ud_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 排序->按照随笔
	 * @return
	 */
	@RequestMapping(value = "/findUdByNote", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findUdByNote(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<UserDetailVO> ud_list = userDetailService.findUdByNote(pageInfo);
		result.getResultParm().put("userDetail_list", ud_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 排序->按照采纳率
	 * @return
	 */
	@RequestMapping(value = "/findUdByAccept", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findUdByAccept(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<UserDetailVO> ud_list = userDetailService.findUdByAccept(pageInfo);
		result.getResultParm().put("userDetail_list", ud_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 搜索
	 * @return
	 */
	@RequestMapping(value = "/searchBlog", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> searchBlog(PageInfo pageInfo,String search){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<UserDetailVO> ud_list = userDetailService.searchBlog(pageInfo,search);
		result.getResultParm().put("userDetail_list", ud_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 根据position_id搜索博客
	 * @return
	 */
	@RequestMapping(value = "/searchBlogByPosition", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> searchBlogByPosition(PageInfo pageInfo,Integer position_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<UserDetailVO> ud_list = userDetailService.searchBlogByPosition(pageInfo,position_id);
		result.getResultParm().put("userDetail_list", ud_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
}
